/*
 * Project Name: cms_web
 * File Name: CustomTagHtmlAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月26日 上午10:46:08
 */
package com.shishuo.cms.action.manage;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.CustomTagHtml;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.CustomTagHtmlService;

@Controller
@RequestMapping("/admin/customTagHtml")
public class CustomTagHtmlAction extends AdminBaseAction
{
	@Autowired
	private CustomTagHtmlService customTagHtmlService;
	@InitBinder("customTagHtml")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("customTagHtml.");
	}
	
	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String add(ModelMap modelMap)
	{
		return "system/customTagHtml/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewcustomTagHtml(@ModelAttribute CustomTagHtml customTagHtml) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		//验证填写的是否正确
		if (customTagHtml.getName().equals("")) {
			json.getErrors().put("name", "名称不能为空");
		}
		if (customTagHtmlService.isExistByname(customTagHtml.getName())==true) {
			json.getErrors().put("name", this.getMsgByMsgKey("system.customTagHtmlAliasHasExist"));
		}
		
		if (validateJsonIsError(json)) return json;
		customTagHtmlService.insert(customTagHtml);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", customTagHtmlService.getAllListPage(pageNum));
		return "system/customTagHtml/list";
	}
	
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "id", defaultValue = "0") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		CustomTagHtml customTagHtml=customTagHtmlService.getById(id);
		modelMap.put("customTagHtml", customTagHtml);
		
		return "system/customTagHtml/update";
	}

	/**
	 * 
	 */
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> deletecustomTagHtml(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		customTagHtmlService.deleteById(id);
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> editcustomTagHtml(@ModelAttribute CustomTagHtml customTagHtml)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		CustomTagHtml customTagHtmlOld = customTagHtmlService.getById(customTagHtml.getId());
		customTagHtmlOld.setName(customTagHtml.getName());
		customTagHtmlOld.setHtmlContent(customTagHtml.getHtmlContent());
		customTagHtmlOld.setLastUpdateDate(new Date());
		customTagHtmlService.update(customTagHtmlOld);
		json.setResult(true);
		return json;
	}
	
}
